<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class ContactForm extends CFormModel
{
	public $message;
	public $email;
	public $subject;
//	public $body;
//	public $verifyCode;

	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			// name, email, subject and body are required
			array('message, email, subject', 'required'),
			// email has to be a valid email address
			array('email', 'email'),
			// verifyCode needs to be entered correctly
//			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
                                                                              array('email, subject, message', 'check'),

		);
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
//		return array(
//			'verifyCode'=>'Verification Code',
//		);
	}
       public function check()
	{
//		if(!$this->hasErrors())
//		{
////			$this->_identity=new UserIdentity($this->username,$this->password);
//                                                         	if(!$this->authenticate())
//				$this->addError('password','Incorrect username or password.');
//		}
        if (!$this->email) {
            $this->addError('email','Xin hãy nhập email');
        }
        if(!$this->subject) $this->addError('subject','Xin hãy nhập chủ đề');
        if(!$this->message) $this->addError('message','Xin hãy nhập nội dung');
	}
        public function contact() {
                if(!$this->hasErrors()) {
                    $mail = new YiiMailMessage;
                        $mail->setBody("Ý kiến khách hàng {$this->email}: <br>
                            {$this->message}", 'text/html');
                        $mail->subject =  $this->subject;
                        $mail->addTo(Yii::app()->params['adminEmail']);
                        $mail->setFrom(array(Yii::app()->params['adminEmail'] => 'Customer'));    //Yii::app()->params['adminEmail'];
                        Yii::app()->mail->send($mail);    
                     return true;
                }
 else {
                                        return FALSE;}
            
        }
        
}